using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace Beetle;

public class DataReader : IDataReader
{
	private bool a = true;

	public Encoding Coding = Encoding.UTF8;

	private Stream b;

	private byte[] c = new byte[8];

	[CompilerGenerated]
	private IChannel d;

	public bool LittleEndian
	{
		get
		{
			return a;
		}
		set
		{
			a = value;
		}
	}

	public long Position
	{
		get
		{
			return b.Position;
		}
		set
		{
			b.Position = value;
		}
	}

	public long Length => b.Length;

	public IChannel Channel
	{
		[CompilerGenerated]
		get
		{
			return d;
		}
		[CompilerGenerated]
		set
		{
			d = value;
		}
	}

	public void Reset()
	{
	}

	public DataReader(Stream stream, bool littleEndian)
	{
		b = stream;
		a = littleEndian;
	}

	public void SetStream(Stream stream)
	{
		b = stream;
	}

	public int ReadByte()
	{
		return b.ReadByte();
	}

	public int Read(byte[] buffer, int offset, int count)
	{
		return b.Read(buffer, offset, count);
	}

	public void Read(ByteArraySegment sagment)
	{
		int num = 0;
		num = ReadInt();
		num = Read(sagment.Array, 0, num);
		sagment.SetInfo(0, num);
	}

	public bool ReadBool()
	{
		if (ReadByte() != 0)
		{
			return true;
		}
		return false;
	}

	public short ReadShort()
	{
		Read(c, 0, 2);
		short num = BitConverter.ToInt16(c, 0);
		if (!a)
		{
			num = j.a(num);
		}
		return num;
	}

	public int ReadInt()
	{
		Read(c, 0, 4);
		int num = BitConverter.ToInt32(c, 0);
		if (!a)
		{
			num = j.a(num);
		}
		return num;
	}

	public long ReadLong()
	{
		Read(c, 0, 8);
		long num = BitConverter.ToInt64(c, 0);
		if (!a)
		{
			num = j.a(num);
		}
		return num;
	}

	public ushort ReadUShort()
	{
		Read(c, 0, 2);
		ushort num = BitConverter.ToUInt16(c, 0);
		if (!a)
		{
			num = j.a(num);
		}
		return num;
	}

	public uint ReadUInt()
	{
		Read(c, 0, 4);
		uint num = BitConverter.ToUInt32(c, 0);
		if (!a)
		{
			num = j.a(num);
		}
		return num;
	}

	public ulong ReadULong()
	{
		Read(c, 0, 8);
		ulong num = BitConverter.ToUInt64(c, 0);
		if (!a)
		{
			num = j.a(num);
		}
		return num;
	}

	public char ReadChar()
	{
		return (char)ReadShort();
	}

	public DateTime ReadDateTime()
	{
		return new DateTime(ReadLong());
	}

	public unsafe float ReadFloat()
	{
		int num = ReadInt();
		return *(float*)(&num);
	}

	public unsafe double ReadDouble()
	{
		long num = ReadLong();
		return *(double*)(&num);
	}

	public void Read(IMessage msg)
	{
		msg.Load(this);
	}

	public T ReadMessage<T>() where T : IMessage, new()
	{
		T val = new T();
		Read(val);
		return val;
	}

	public IList<T> ReadMessages<T>() where T : IMessage, new()
	{
		int num = ReadInt();
		List<T> list = new List<T>(num);
		for (int i = 0; i < num; i++)
		{
			T item = new T();
			item.Load(this);
			list.Add(item);
		}
		return list;
	}

	public string ReadString(int length)
	{
		if (length <= 0)
		{
			return null;
		}
		return x.f().b(length, Coding, b);
	}

	public string ReadString()
	{
		int length = ReadInt();
		return ReadString(length);
	}

	public string ReadUTF()
	{
		ushort length = ReadUShort();
		return ReadString(length);
	}
}
